package parsers;

import java.util.List;

import dao.Linker;

import model.Group;
import model.LectureRoom;
import model.Teacher;
import net.htmlparser.jericho.Element;

public class CommonMethods
{
	public static String checkSpace(String s)
	{
		if(s.contains("&nbsp"))
			return "";
		else
			return s;
	}
	
	public static String parseUnwantedMarkers(String s)
	{
		return s.replace("</t>", "").replace("<t>", "").replace("<T>", "").replace("</T>", "").replace("<b>", "").replace("</b>", "").replace("</h3>", "").replace("<h3>", "").replace("<B>", "").replace("</B>", "").replace("</H3>", "").replace("<H3>", "").trim();
	}

	public static String parseLink(String link)
	{
		return link.replace("&pTyp=P", "");
	}
	
	public static void parseLectureRooms(Element e, model.student.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			LectureRoom lr = subject.createLectureRoom(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			lr.setLink(CommonMethods.parseLink(link));
			
			Linker.lectureRoomRAWList.add(lr);
		}
	}
	
	public static void parseLectureRooms(Element e, model.teacher.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			LectureRoom lr = subject.createLectureRoom(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			lr.setLink(CommonMethods.parseLink(link));
			
			Linker.lectureRoomRAWList.add(lr);
		}
	}
	
	public static void parseLectureRooms(Element e, model.reservation.teacher.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			LectureRoom lr = subject.createLectureRoom(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			lr.setLink(CommonMethods.parseLink(link));
			
			Linker.lectureRoomRAWList.add(lr);
		}
	}
	
	public static void parseGroups(Element e, model.teacher.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Group g = subject.createGroup(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			g.setLink(CommonMethods.parseLink(link));
			
			Linker.groupRAWList.add(g);
		}
	}
	
	public static void parseGroups(Element e, model.reservation.teacher.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Group g = subject.createGroup(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			g.setLink(CommonMethods.parseLink(link));
			
			Linker.groupRAWList.add(g);
		}
	}
	
	public static void parseGroups(Element e, model.reservation.lectureRoom.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Group g = subject.createGroup(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			g.setLink(CommonMethods.parseLink(link));
			
			Linker.groupRAWList.add(g);
		}
	}
	
	public static void parseGroups(Element e, model.lectureRoom.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Group g = subject.createGroup(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			g.setLink(CommonMethods.parseLink(link));
			
			Linker.groupRAWList.add(g);
		}
	}
	
	public static void parseTeachers(Element e, model.lectureRoom.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Teacher t = subject.createTeacher(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			t.setLink(CommonMethods.parseLink(link));
			
			Linker.teacherRAWList.add(t);
		}
	}
	
	public static void parseTeachers(Element e, model.reservation.lectureRoom.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Teacher t = subject.createTeacher(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			t.setLink(CommonMethods.parseLink(link));
			
			Linker.teacherRAWList.add(t);
		}
	}
	
	public static void parseTeachers(Element e, model.student.Subject subject)
	{
		List<Element> list = e.getAllElements("a");
		for (int i = 0; i < list.size(); i++)
		{
			Teacher t = subject.createTeacher(list.get(i).getContent().toString());
			String link = list.get(i).getAttributeValue("href");
			t.setLink(CommonMethods.parseLink(link));
			
			Linker.teacherRAWList.add(t);
		}
	}
	
	public static String parseElement(Element e)
	{
		List<Element> list = e.getAllElements("a");
		String elements = "";
		for (int i = 0; i < list.size(); i++)
		{
			if(!elements.contains(list.get(i).getContent()))
			{
				elements += list.get(i).getContent();
				if(i < list.size() - 1)
					elements += "; ";
			}
		}
		
		String s = e.getContent().toString();
		s = s.substring(s.lastIndexOf("</a>") + 4, s.length());
		
		if(s.length() > 0)
			elements += s;
		
		return elements;
	}
	
	public static boolean checkDay(String string)
	{
		if(string.equals("Poniedziałek"))
			return true;
		else if(string.equals("Wtorek"))
			return true;
		else if(string.equals("Środa"))
			return true;
		else if(string.equals("Czwartek"))
			return true;
		else if(string.equals("Piątek"))
			return true;
		else if(string.equals("Sobota"))
			return true;
		else if(string.equals("Niedziela"))
			return true;
		else if(string.equals("Nieregularne"))
			return true;
		else
			return false;
	}
}
